package com.sihan.framework.etm.common.util;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

/**
 * 判断坐标是否是 十进制度（DD）格式
 * 例如:103.98448972,30.41091145
 */
public class DecimalDegreeValidator {
    public static boolean isValidDecimalDegree(String input) {
        String decimalDegreePattern = "[-]?\\d+(\\.\\d+)?";
        Pattern pattern = Pattern.compile(decimalDegreePattern);
        Matcher matcher = pattern.matcher(input);
        return matcher.matches();
    }

    public static boolean isLatitudeValid(double latitude) {
        return latitude >= -90.0 && latitude <= 90.0;
    }

    public static boolean isLongitudeValid(double longitude) {
        return longitude >= -180.0 && longitude <= 180.0;
    }

    /**
     * 判断坐标是否是 十进制度（DD）格式
     * 例如:103.98448972,30.41091145
     * @param lntLet 经纬度
     * @return
     */
    public static boolean isRational(String lntLet){
        String[] coordinates = lntLet.split(",");
        if (coordinates.length == 2) {
            boolean isValidLongitude = isValidDecimalDegree(coordinates[0].trim());
            boolean isValidLatitude = isValidDecimalDegree(coordinates[1].trim());
            if (isValidLongitude && isValidLatitude) {
                double longitude = Double.parseDouble(coordinates[0].trim());
                double latitude = Double.parseDouble(coordinates[1].trim());
                if (isLongitudeValid(longitude) && isLatitudeValid(latitude)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        String input = "10398448.972,30.41091145";
        String[] coordinates = input.split(",");

        if (coordinates.length == 2) {
            boolean isValidLongitude = isValidDecimalDegree(coordinates[0].trim());
            boolean isValidLatitude = isValidDecimalDegree(coordinates[1].trim());

            if (isValidLongitude && isValidLatitude) {
                double longitude = Double.parseDouble(coordinates[0].trim());
                double latitude = Double.parseDouble(coordinates[1].trim());

                if (isLongitudeValid(longitude) && isLatitudeValid(latitude)) {
                    System.out.println("输入的经纬度格式正确且合理");
                } else {
                    System.out.println("输入的经纬度格式正确，但经纬度值不合理");
                }
            } else {
                System.out.println("输入的经纬度格式不正确");
            }
        } else {
            System.out.println("输入的经纬度格式不正确");
        }
    }
}
